<template>
  <div>
    <div class="crud-group">
      <el-input
        v-model="paramsForm.clusterFlag"
        placeholder="所属集群标识"
        clearable
      />
      <el-select
        v-model="paramsForm.nasId"
        clearable
        size="small"
        placeholder="Nas名称"
        class="filter-item"
      >
        <el-option
          v-for="item in nasList"
          :key="item.key"
          :label="item.value"
          :value="item.key"
        />
      </el-select>
      <el-input
        v-model="paramsForm.serverIp"
        placeholder="服务器IP"
        clearable
      />
      <el-input
        v-model="paramsForm.mountPath"
        placeholder="挂载路径"
        clearable
      />
      <el-select
        v-model="paramsForm.isEnable"
        placeholder="是否启用"
        clearable
      >
        <el-option
          v-for="item in [{label: '是', value: 1}, {label: '否', value: 0}]"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <div>
        <el-button
          type="success"
          icon="el-icon-search"
          @click="getData"
        >
          搜索
        </el-button>
        <el-button
          type="warning"
          icon="el-icon-refresh-left"
          @click="reset"
        >
          重制
        </el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addDialogVisible = true"
        >
          新增
        </el-button>
      </div>
    </div>

    <el-table
      v-loading="loading"
      border
      :data="data"
    >
      <el-table-column
        label="所属集群"
        prop="clusterFlag"
      />
      <el-table-column
        label="Nas名称"
        prop="nasName"
      />
      <el-table-column
        label="服务器IP"
        prop="serverIp"
      />
      <el-table-column
        label="挂载路径"
        prop="mountPath"
      />
      <el-table-column
        label="启用状态"
        prop="releaseType"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
            active-value="1"
            inactive-color="#ff4949"
            inactive-value="2"
            @change="(val)=>{
              toggle(scope.row)
            }"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="100px"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="()=>{
              selectedRow = Object.assign({}, scope.row)
              addDialogVisible = true
            }"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            @click="()=>{
              del(scope.row.id)
            }"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        layout="prev, pager, next,sizes,total"
        :page-sizes="[5, 10, 20, 30, 40]"
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        :total="total"
        @size-change="()=>{
          paramsForm.size = val
          getData()
        }"
        @current-change="()=>{
          paramsForm.current = val
          getData(false)
        }"
      />
    </div>

    <AddDialog
      :dialog-visible="addDialogVisible"
      :edit-form="selectedRow"
      @close="()=>{
        addDialogVisible = false
        selectedRow = null
        getData()
      }"
    />
  </div>
</template>

<script>
import coldApi from '@/api/coldStandby/index'
import AddDialog from './AddDialog.vue'
const form = {
  current: 1,
  size: 10,
  nasId: '',
  serverIp: '',
  mountPath: '',
  isEnable: ''
}
export default {
  components: {
    AddDialog
  },
  data() {
    return {
      loading: false,
      addDialogVisible: false,
      selectedRow: null,
      total: 0,
      paramsForm: {...form},
      data: [],
      nasList: []
    }
  },
  created() {
    this.getData()
    this.initData()
  },
  methods: {
    getData(resetPage = true) {
      if (resetPage) {
        this.paramsForm.current = 1
      }
      this.loading = true
      coldApi.getNasAgentConfig(this.paramsForm).then(res => {
        console.log(res)
        this.data = res.content
        this.total = res.totalElements
      }).finally(() => {
        this.loading = false
      })
    },
    initData() {
      this.nasList = []
      coldApi.getUsefulNas().then(res => {
        res.forEach((array) => {
          this.nasList.push({ key: array.id, value: array.nasName })
        })
      })
    },
    reset() {
      this.paramsForm = {...form}
      this.getData()
    },
    del(id) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        coldApi.delNasAgentConfig(id).then(res => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getData()
        })
        // delete
      })
    },
    // 改变是否禁用状态
    toggle(row) {
      coldApi.toggleNasAgentStatus(row.id).then(async (res) => {
        this.$notify({
          title: '提示',
          message: '修改状态成功',
          type: 'success'
        })
        await this.getData()
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
